#include "StoneEntity.h"
#include "EffectManager.h"




StoneEntity::StoneEntity(int id, int type, double x, double y)
{
	_data = new StoneData(id, type, x, y);
	_count = 0;
	life = 5;
	_uninteractTime = 0;
	_delayTime = 0;
	_x0 = x;
	_y0 = y;
}

void StoneEntity::update(long time)
{
	if (_data->isDead) return;
	_delayTime++;
	if(_delayTime >= DELAY_TIME)
	{
		if (_data->getRect()->Vy > 0) 
			_data->getRect()->Vy -= 0.8f;
		else 
			_data->getRect()->Vy = GRAVITY + 7;
		_data->getRect()->Y += _data->getRect()->Vy;

		_data->sprites->updateCurrent();
	}
	else
	{
		if(_delayTime % 6 == 0)
		{
			_data->getRect()->Vx = 1;
		}
		else if(_delayTime % 3 == 0)
		{
			_data->getRect()->Vx = -1;
		}

		_data->getRect()->X += _data->getRect()->Vx;
	}
	if(_data->getRect()->Y <= GameDeviceSingleton::getInstance()->getViewPort()->getTop())
	{
		_count++;
	}
	if(_count == RECREATE_TIME)
	{
		_recreate();
	}

	if(_uninteractTime > 0)
	{
		if(_uninteractTime == UNINTERACT_TIME)
		{
			_data->canInteract = true;
			_uninteractTime= 0;
		}
		else
		{
			_uninteractTime++;
		}
	}
}

void StoneEntity::updateData(int actions, int effect)
{
	if(actions == ACTIONS_STONE_IN_LAND)
	{
		_data->getRect()->Vy += 10;
		_data->getRect()->Y += _data->getRect()->Vy;
		_data->canInteract = false;
		_uninteractTime = 1;
	}
	if(actions == ACTIONS_IS_HURT) {
		life -= effect;
		if (life <= 0 && !_data->isDead) {
			_data->isDead = true;
			_data->canInteract = false;
			EffectManager::getInstatnse()->createEffect(EffectType::TYPE_Object_Fire, _data->rect->X, _data->rect->Y);
		}
	}
}

AbstractData* StoneEntity::getAbstractData()
{
	return _data;
}

void StoneEntity::draw(long time)
{
	if (_data->isDead) return;
	_data->sprites->draw();
}

StoneEntity::~StoneEntity()
{

}

void StoneEntity::_recreate()
{
	_delayTime = 0;
	_count = 0;
	_data->getRect()->X = _x0;
	_data->getRect()->Y = _y0;
}
